package com.lzy.cloud.goods.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.dtos.CarSaleDto;
import com.lzy.cloud.goods.facade.CarSaleFacade;
import com.lzy.cloud.goods.request.CarSaleRequest;
import com.lzy.cloud.goods.response.CarSaleResponse;
import com.lzy.cloud.goods.service.CarDraftService;
import com.lzy.cloud.goods.service.CarSaleService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by WIN7 on 2018/6/11.
 */
@RestController
@RequestMapping("/goods/car/sale")
public class CarSaleController {

    @Autowired
    CarSaleFacade carSaleFacade;

    @Autowired
    CarSaleService carSaleService;

    /**
     * 新增车型
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody(required = false) CarSaleRequest request){
        Ensure.that(request).isNotNull("00010001");
        return new Result(carSaleFacade.add(DozerUtils.map(request, CarSaleDto.class)));
    }

    /**
     * 新增车型草稿
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add/draft",method = RequestMethod.POST)
    public Result addDraft(@RequestBody(required = false) CarSaleRequest request){
        Ensure.that(request).isNotNull("00010001");
        return new Result(carSaleFacade.addDraft(DozerUtils.map(request, CarSaleDto.class)));
    }

    /**
     * 编辑车型
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result update(@RequestBody(required = false) CarSaleRequest request){
        Ensure.that(request).isNotNull("00010001");
        return new Result(carSaleFacade.update(DozerUtils.map(request, CarSaleDto.class)));
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.GET)
    public Result selectById(@PathVariable("id") Long id){
        CarSaleDto carSaleDto = carSaleFacade.getDetail(id);
        Ensure.that(carSaleDto).isNotNull("00020004");
        return new Result(DozerUtils.map(carSaleDto, CarSaleResponse.class));
    }

    /**
     * 分页查询车型列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list" ,method = RequestMethod.GET)
    public Result selectById(CarSaleRequest request){
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<CarSaleDto> pageInfo = carSaleService.selectList(pager,DozerUtils.map(request,CarSaleDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), CarSaleResponse.class));
        } else {
            return new Result();
        }
    }


}
